<template>
  <div class="login-container">
    <el-form :model="ruleForm" :rules="rules"
             status-icon
             ref="ruleForm"
             label-position="left"
             label-width="0px"
             class="demo-ruleForm login-page">
      <div style="display: flex;justify-content: center">
        <h2 class="title">管理系统基础框架</h2>
      </div>
      <el-form-item prop="username">
        <el-input type="text"
                  v-model="ruleForm.username"
                  auto-complete="off"
                  placeholder="用户名">
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password"
                  v-model="ruleForm.password"
                  auto-complete="off"
                  placeholder="密码">
        </el-input>
      </el-form-item>
      <el-form-item prop="vercode">
        <div style="display: flex">
          <el-input type="vercode"
                    v-model="ruleForm.vercode"
                    auto-complete="off"
                    placeholder="验证码">
          </el-input>
          <el-image style="width: 150px; height: 50px;margin-top:-5px" :src="imageCodeBase64" fit="fill"/>
        </div>
      </el-form-item>
<!--      <el-checkbox-->
<!--          v-model="checked"-->
<!--          class="remember">-->
<!--        记住密码-->
<!--      </el-checkbox>-->
      <el-form-item style="width:100%;">
        <el-button type="primary" style="width:100%;" @click="submitForm('ruleForm')" :loading="loginLoading">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapMutations } from 'vuex'
export default {
  name:'Login',
  data() {
    let username = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入账号'));
      } else {
        if (this.ruleForm.password !== '') {
          this.$refs.ruleForm.validateField('password');
        }
        callback();
      }
    };
    let password = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      }else {
        callback();
      }
    };
    let vercode = (rule, value, callback)=>{
      if(value===''){
        callback(new Error('请输入验证码'));
      }else{
        callback();
      }
    }
    return {
      loginLoading: false,
      checked: false,
      imageCodeBase64:'',
      ruleForm: {
        username: '',
        password: '',
        verkey:'',
        vercode:''
      },
      rules: {
        username: [
          { validator: username, trigger: 'blur' }
        ],
        password: [
          { validator: password, trigger: 'blur' }
        ],
        vercode:[
          { validator: vercode, trigger: 'blur' }
        ]
      }
    };
  },
  created(){
    this.getImageCodeBase64();
  },
  methods: {
    ...mapMutations([`setToken`, `setUserId`, `setUserInfo`, `setCurrentActiveMenu`]),
    //获取验证码
    getImageCodeBase64(){
      let url = "images/captcha";
      this.$http.get(url).then(res=>{
        this.ruleForm.verkey = res.data.data.ver_key
        this.imageCodeBase64 = res.data.data.ver_code
      })
    },
    //提交登录
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'login'
          let user = {}
          user.username = this.ruleForm.username
          user.password = this.ruleForm.password
          user.verKey = this.ruleForm.verkey
          user.verCode = this.ruleForm.vercode
          this.$http.post(url,user).then(res => {
            if(res.data.code===0){
              let token = res.data.data.token
              this.setToken(token)
              let userId = res.data.data.id
              this.setUserId(userId)
              let userInfo = res.data.data.user
              this.setUserInfo(userInfo)
              this.$router.push("/index")
            }else{
              this.getImageCodeBase64()
              this.$message.error(res.data.message)
            }
          })
        } else {
          return false;
        }
      });
    },
    //清空表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.login-container {
  width: 100%;
  height: 100%;
}
.login-page {
  -webkit-border-radius: 5px;
  border-radius: 5px;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}
label.el-checkbox.remember {
  margin: 0px 0px 15px;
  text-align: left;
}
</style>
